package org.lee;

import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author jackielee
 * @version 1.0
 * @description AppDoc
 * @date 2021/6/1
 */
public class AppDoc {

  public static void main1(String[] args) {
    String iFile = "C:\\Users\\Jackie\\Downloads\\dd\\DevOps流程建设图.docx";
    iFile = "C:\\Users\\Jackie\\Downloads\\d541c5ef-be75-4114-a9fc-1b04c9bbed24.docx";
    try (FileInputStream fis = new FileInputStream(iFile)) {
      XWPFDocument doc = new XWPFDocument(fis);
      int i = 1;
      for (XWPFPictureData pic : doc.getAllPackagePictures()) { // doc.getAllPictures()) {
        //        ByteArrayInputStream b = new ByteArrayInputStream(pic.getData());
        FileOutputStream fout =
            new FileOutputStream(
                String.format("C:\\Users\\Jackie\\Downloads\\dd\\doc\\DevOps流程建设图-%d.png", i++));
        fout.write(pic.getData());
      }

      XWPFWordExtractor extractor = new XWPFWordExtractor(doc);

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  //  public static void main(String[] args) throws IOException {
  //    Document document = new Document();
  //    document.loadFromFile("C:\\Users\\Jackie\\Downloads\\dd\\DevOps流程建设图.docx");
  //    String text = document.getText();
  //    System.out.println(text);
  //
  //    Queue nodes = new LinkedList();
  //    nodes.add(document);
  //
  //    List<BufferedImage> images = new ArrayList();
  //    while (nodes.size() > 0) {
  //      ICompositeObject node = (ICompositeObject) nodes.poll();
  //      for (int i = 0; i < node.getChildObjects().getCount(); ++i) {
  //        IDocumentObject child = node.getChildObjects().get(i);
  //        if (child instanceof ICompositeObject) {
  //          nodes.add(child);
  //          // get each image and add it to the list
  //          if (child.getDocumentObjectType() == DocumentObjectType.Picture) {
  //            DocPicture picture = (DocPicture) child;
  //            images.add(picture.getImage());
  //          }
  //        }
  //      }
  //    }
  //    document.close();
  //    for (int i = 0; i < images.size(); ++i) {
  //      File file =
  //          new File(String.format("C:\\Users\\Jackie\\Downloads\\dd\\doc\\DevOps流程建设图-%d.png",
  // i));
  //      ImageIO.write(images.get(i), "png", file);
  //    }
  //  }
}
